//
//  CoreLocationExampleViewController.m
//  CoreLocationExample
//

#import "CoreLocationExampleViewController.h"

#define DCLog(format, ...) [self logToScreen:[NSString stringWithFormat:format, ## __VA_ARGS__]];

@implementation CoreLocationExampleViewController

- (void)logToScreen:(NSString *)output {
	
	CGPoint pt = logView.contentOffset;
	
	// 메시지를 출력한다.
	logView.text = [NSString stringWithFormat:@"%@\n%@", logView.text, output];
	
	// 스크롤 위치를 조정한다.
	[logView setContentOffset:pt animated:NO];
	[logView scrollRangeToVisible:NSMakeRange([logView.text length], 0)];
}

- (void)viewDidLoad {
    [super viewDidLoad];

	// 위치 정보를 출력할 뷰 생성.
	logView = [[UITextView alloc] initWithFrame:[self.view bounds]];
	logView.editable = NO;
	logView.userInteractionEnabled = YES;
	[self.view addSubview:logView];
	
	lm = [[CLLocationManager alloc] init];
	lm.delegate = self;
	[lm startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLoc
		   fromLocation:(CLLocation *)oldLoc {
	DCLog(@"location found: %@", newLoc.description);
	[lm stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
	   didFailWithError:(NSError *)error {
	DCLog(@"location failed with error: %@", error);
	[lm stopUpdatingLocation];
}

- (void)dealloc {
	[logView release];
	[lm release];
    [super dealloc];
}

@end
